package com.example.boot3mongodb.controller;

import com.example.boot3mongodb.entity.Book;
import com.example.boot3mongodb.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;


@RestController
public class BookController {

    @Autowired
    BookService bookService;

    @RequestMapping("/save")
    @ResponseBody
    public String save(String id,String name,int price){
        Book book = new Book();
        book.setCreateTime(new Date());
        book.setUpdateTime(new Date());
        book.setId(id);
        book.setName(name);
        book.setInfo("202312info");
        book.setPrice(price);
        book.setPublish("202312publish");
        bookService.saveObj(book);
        return  "save book success!";
    }

    @RequestMapping("/find")
    @ResponseBody
    public String find(){
        List<Book> books = bookService.findAll();
        if (books != null && books.size()>0) {
            System.out.println();
            for (int i = 0; i < books.size(); i++) {
                System.out.println( "书籍名称："+books.get(i));
            }
        }
        return  "save book success!";
    }





}
